package com.huabo.cybermonitor.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.JsonBean;
import com.huabo.cybermonitor.entity.MonitorSolution;
import com.huabo.cybermonitor.mapper.YhrPageMapper;
import com.huabo.cybermonitor.service.*;
import com.huabo.cybermonitor.task.ZNJKGZYJTask;
import com.huabo.cybermonitor.task.ZNJKMXYJTask;
import com.huabo.cybermonitor.task.ZNJKZBYJTask;
import com.huabo.cybermonitor.task.base.JobTaskService;
import com.huabo.cybermonitor.util.ConstClass;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@Slf4j
@Api(value = "监控执行-指标执行", tags = {"监控执行-指标执行所有接口"})
@RequestMapping(value = "/cyber/ZhiBiaoZhiXingController")
@SuppressWarnings("all")
public class ZhiBiaoZhiXingController {

    private static final Logger logger = LoggerFactory.getLogger(ZhiBiaoZhiXingController.class);

    @Autowired
    IOrganizationService iOrganizationService;

    @Autowired
    IMonitorModelsolutionService iMonitorModelsolutionService;

    @Autowired
    IMonitorExeintervalService iMonitorExeintervalService;

    @Autowired
    IMonitorSolutionRuleService iMonitorSolutionRuleService;

    @Autowired
    IMonitorSolutionService iMonitorSolutionService;

    @Autowired
    IWorksheetService iWorksheetService;

    @Autowired
    IAttachmentService iAttachmentService;

    @Autowired
    public ZNJKZBYJTask znjkzbyjTask;

    @Autowired
    public ZNJKGZYJTask znjkgzyjTask;

    @Autowired
    public JobTaskService jobTaskService;

    @Autowired
    IStaffService iStaffService;

    @Autowired
    IBugCriterionService iBugCriterionService;

    @Autowired
    TreeService treeService;

    @Autowired
    IMonitorModelService iMonitorModelService;

    @Autowired
    IIndicatorService iIndicatorService;

    @Resource
    public ZNJKMXYJTask znjkmxyjTask;

    @Resource
    public IMonitorRuleService iMonitorRuleService;

    @Resource
    YhrPageMapper pageMapper;


    /**
     * 监控执行：指标预警-列表
     *
     */
    @ApiOperation("监控执行：指标执行 --右侧指标执行列表分页查询")
    @PostMapping(value = "/jkjg/kri_zhibiao_info")
    public JsonBean kri_zhibiao_info(@ApiParam(name = "orgId", value = "orgId") @RequestParam(value = "orgId", required = false) BigDecimal orgId,
                                     @ApiParam(name = "pageNumber", value = "pageNumber") @RequestParam(value = "pageNumber",required = false,defaultValue = "1") Integer pageNumber,
                                     @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }

        TblStaffUtil staff = null;
        try {
            staff = DealUserToken.parseUserToken(token);
            orgId = Objects.isNull(orgId)?staff.getLinkDetp().getOrgid(): orgId;

            Boolean isSelect = treeService.isSJByOrgId(staff.getLinkDetp().getOrgid().toString(), orgId.toString());

            StringBuilder sb=new  StringBuilder();

            sb.append("select t.*,REALNAME,USERNAME from TBL_MONITOR_SOLUTION  t left join TBL_STAFF on t.STAFFID = TBL_STAFF.STAFFID ")
                    .append(" where t.type=2 and t.SOLUTIONSTATUS='启用' ");
            if (isSelect) {
                sb.append(" and t.orgid="+orgId);

            } else {
                sb.append(" and t.orgid="+ staff.getLinkDetp().getOrgid());

            }
            sb.append(" order by t.createdate desc");

            IPage<Map<String,Object>>  page =new Page<>(pageNumber,ConstClass.DEFAULT_SIZE);
            page=pageMapper.getPage(page,sb.toString());

            Map<String, Object> map = new HashMap<>(3);
            map.put("orgId", orgId);
            map.put("pageBean", page);
            map.put("tblStaff", staff);
            return new JsonBean(200, "success", map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonBean(200,"failure","");
    }


    /**
     * 指标预警 --结果
     *
     * @author SongXiangYing
     * @date 2016年1月31日 上午2:18:03
     * @param pageNumber
     * @param request
     * @return
     */
    @ApiOperation("指标执行 --结果")
    @GetMapping(value = "/zbjk/kri_yj_info_result")
    public JsonBean kri_info_yj_reult(
            @ApiParam(name = "orgId", value = "orgId") @RequestParam(value = "orgId", required = false) BigDecimal orgId,
            @ApiParam(name = "pageNumber", value = "pageNumber") @RequestParam(value = "pageNumber",required = false,defaultValue = "1") Integer pageNumber,
            @ApiParam(name = "id", value = "id") @RequestParam(value = "id", required = false) BigDecimal id,
            @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }

        String sql="select t.*,TBL_STAFF.REALNAME,TBL_STAFF.USERNAME from TBL_MONITOR_INDICATORRESULT  t left join  TBL_STAFF  on t.STAFFID = TBL_STAFF.STAFFID where t.RESULTID in (select max(RESULTID) from TBL_MONITOR_INDICATORRESULT  GROUP BY EXECUTEID ) and t.SOLUTIONRESULTID = "+id+" ORDER BY t.SAVETIME DESC";
        // pageBean = this.tblIndicatorService.findByYjId(new BigDecimal(id),
        // pageNumber, pageBean.getPageSize());


        IPage<Map<String,Object>>  page =new Page<>(pageNumber,ConstClass.DEFAULT_SIZE);
        page=pageMapper.getPage(page,sql);

        Map<String, Object> map = new LinkedHashMap<>(3);
        map.put("orgId", orgId);
        map.put("id", id);
        map.put("pageBean", page);

        return new JsonBean(200, "success", map);
    }


}
